package cn.com.xy.sms.sdk.ui.popu.widget;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.com.xy.sms.sdk.ui.R;
import cn.com.xy.sms.sdk.util.DuoquUtils;

public class ServiceMenuDialog {  
    public static void showMenuDialog(final Context context, JSONObject jobj, Map map) {
        SelectDialog dlg = new SelectDialog(jobj, context);
        dlg.ShowDialog();
    }
    

    public static class SelectDialog {
        private JSONArray       mActionData;
        private Context         mContext;
        private List<View>      mAllSelects = null;
        private TextView        mLeft;
        private AlertDialog     mAlertDialog;
        public DialogParams     params;
        
        
        public SelectDialog(JSONObject jObject, Context context) {
            super();
            mActionData = (JSONArray) jObject.opt("action_data");
            mContext = context;
            mAllSelects = new ArrayList<View>();
 
            params = new DialogParams();
            params.mDefaultTextColor = R.color.duoqu_theme_color_3010;
            params.mSelectTextColor = R.color.duoqu_theme_color_3010;
            params.mDefaultTitleName = jObject.optString("extract_text");
        }
        
        private void setItemViewBg(View view, int index, int count) {
            if(view == null)
                return;
            
            view.setBackgroundResource(R.drawable.duoqu_group_navi_item_mid_s);

            if (index == count - 1) {
                View sepLine = view.findViewById(R.id.duoqu_list_items_content_part_splite);
                sepLine.setVisibility(View.GONE);
            }
        }
        
        public void ShowDialog() {
            // TODO Auto-generated method stub
            try{
                if (mActionData != null && mActionData.length() > 0) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                    builder.setCancelable(true);
                    View customView = LayoutInflater.from(mContext).inflate(R.layout.duoqu_service_menu_dialog, null);
                    LinearLayout rootLayout = (LinearLayout) customView.findViewById(R.id.item_roots);
                    
                    for (int position = 0; position < mActionData.length(); position++) {
                        JSONObject itemJson = mActionData.optJSONObject(position);
                        View itemChildView = View.inflate(mContext, R.layout.duoqu_list_items_service_menu_part, null);
                        setItemViewBg(itemChildView, position, mActionData.length());
                        
                        TextView itemText = (TextView) itemChildView.findViewById(R.id.item_text);
                        itemText.setText(itemJson.optString("name"));
                        mAllSelects.add(itemChildView);
                        
                        final String actionData = itemJson.get("action_data").toString();
                        itemChildView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                DuoquUtils.doActionContext(mContext, actionData, null);
                                mAlertDialog.dismiss();
                            }
                        });
                        rootLayout.addView(itemChildView);
                    }
                    mLeft = (TextView) customView.findViewById(R.id.duoqu_select_dialog_left);
                    mLeft.setOnClickListener(new OnClickListener() {
                        
                        @Override
                        public void onClick(View v) {
                            mAlertDialog.dismiss();
                        }
                    });
                    
                    mAlertDialog = builder.create();
                    mAlertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    mAlertDialog.show();
                    mAlertDialog.setContentView(customView);
                    Window window = mAlertDialog.getWindow();
                    window.setGravity(Gravity.BOTTOM);
                    window.getDecorView().setPadding(0, 0, 0, 0);
                    window.setBackgroundDrawableResource(android.R.color.white);
                    WindowManager.LayoutParams lp = window.getAttributes();
                    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    window.setAttributes(lp);
                }
            }catch(Throwable ex){
                
            }
        }
        
        public class DialogParams {
            public int    mDefaultTextColor;
            public int    mSelectTextColor;
            public String mDefaultTitleName;
            public String mSelectItemKey;
        }
    }
    
}
